package com.ktmall.controller;

import com.ktmall.pojo.AddressParams;
import com.ktmall.service.AddressService;
import com.ktmall.utils.RespMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Api(tags = "地址相关接口")
@RestController
@RequestMapping("/uni/member/address")
public class AddressController {
    @Autowired
    private AddressService addressService;
    @ApiOperation("新增地址")
    @PostMapping("/insert")
    public RespMsg insert(@RequestBody AddressParams address){
        int i = addressService.insert(address);
        System.out.println(address.toString());
        if(i > 0){
            return RespMsg.success("新增成功",address);
        }else{
            return RespMsg.fail("新增失败");
        }
    }

    @ApiOperation("根据登录用户查询收货地址")
    @PostMapping("/findByUserId")
    public RespMsg findByUserId(@RequestBody Integer userId){
        List<AddressParams> list = addressService.findByUserId(userId);
        return RespMsg.success("我的收货地址查询成功！",list);
    }

    @ApiOperation("根据登录用户查询地址详情")
    @PostMapping("/findById")
    public RespMsg findById(@RequestBody Integer id){
        AddressParams address = addressService.findById(id);
        return RespMsg.success("地址详情查询成功",address);
    }

    @ApiOperation("修改登录用户地址")
    @PostMapping("/update")
    public RespMsg update(@RequestBody AddressParams address){
        int i = addressService.update(address);
        if(i > 0){
            return RespMsg.success("修改成功！",address);
        }else{
            return RespMsg.fail("修改失败！");
        }
    }

    @ApiOperation("删除登录用户地址")
    @PostMapping("/del")
    public RespMsg del(@RequestBody Integer id){
        int i = addressService.delById(id);
        if(i > 0){
            return RespMsg.success("删除成功");
        }else{
            return RespMsg.fail("删除失败");
        }
    }
}
